<?php

class CTourTableDraw
{

public static $instance; 

public static function getInstance()
	{
		//echo "<div id=register-box>";
        if(self::$instance==null)
		  self::$instance=new CTourTableDraw();
	//   echo "</div>";	  
		return  self::$instance;
	   
    
    }	


    function DrawDivAdBegin(){$tab=func_get_args();if(count(func_get_args())>1){echo "<div id=\"$tab[0]\">";return;} echo "<div class=\"$tab[0]\">";}
    
    function DrawDivAdEnd(){echo "</div>"; }



    function DrawTable($title,$table,$growtable,$campos,$default)//obligatorio ID como campo inicial
    {
    $qu="select ";
    $i = 0;
    foreach($campos as $id=>$valor)
    {
    $qu=$qu."$id,";

    }
    $qu=substr($qu,0,strlen($qu)-1);
  
    
    $qu=$qu." from $growtable inner join $table ON $growtable.TOUR_idTour = $table.idTour WHERE DefaultTourPack=$default order by $growtable.DateBegTour ";


     echo "<div id='content_area_user_title'>";
    echo "<h2> $title</h2>";
    echo "</div>";


    echo	"<form>";						
    echo "<form id='filter-form'>Buscar <input name='filter' id='filter' value='' maxlength='30' size='30' type='text'></form><br>";
    echo "</form>";
   
    echo "<div class='usersettings_statistics'>"; 
 


    echo "<table class='food_planner'>";
    
    

     //echo "<table> <tbody>";

    echo "<thead>";
    echo "<tr>";    
     foreach($campos as $id=>$valor){
  
    echo "<th>$valor</th>";
    
    }
    
    echo "<th>Operador</th>";
    echo "<th>Oficina</th>";
    echo "<th>Pasajeros</th>"; 
    echo "</tr>";    
    echo "</thead>";

echo "<tbody>";
  
   
    // $qu="select*from $table";
    $table=CModelo::getInstance()->getData($qu);
//var_dump($table);
    $cont=0;
    $ruta=CConfig::getInstance()->rutalocal;
    
    
    ///////////////Inicializando la variable NewDate ///////////////////
            
            $time="2000-00-00";
            $newDate=strtotime($time);
            $newDate2=strtotime($time);
            $current_date = date("Y-m-d");
            $today = strtotime($current_date);
        ////////////////////////////////////////////////////////////////////
    
    foreach($table as $campo => $valor)
    { 
    
    if($cont%2){
    $cont = $cont+1;

        $i="";$cont=0;$j=0;
        foreach($valor as $id=>$val){
            if($cont==0){$i=$val;}
            if($cont==1){$paquete=$val;}
            if($cont==4){$newDate=strtotime($val);}
            if($cont==5){$newDate2=strtotime($val);} 
    
                if($j==0){                    
                    if($newDate < $today && $today < $newDate2){    ////  newDate <  $today < NewDate2
                        echo "<tr class='ontransit'>";}
                                                
                    else{
                        echo "<tr class='pending'>";}
                }
                
        echo "<td>$val</td>";$cont=$cont+1;
        $j=1;
        }


$numpax = "select count(*) from tour_has_pasajero where tour_idTour=$i";
$numpaxa = CModelo::getInstance()->getData($numpax);

//Conteo de pasajeros del tour
    foreach($numpaxa as $r=>$c){
        $table=$c;
        foreach($table as $r2=>$c2){
            $numero = $c2;
        }
    }

$tour = "SELECT pack_idPack from pack_has_tour where IdPackTour=$i"; 
$idTour = CModelo::getInstance()->getData($tour);

    foreach($idTour as $r=>$c){
        $table=$c;
        foreach($table as $r2=>$c2){
            $idPack = $c2;
        }
    
    }

    $query = "SELECT  `operador`.`NamOpe` ,  `oficinas`.`idOfi` ,  `oficinas`.`OPERADOR_idOpe` ,  `pack`.`OFICINAS_idOfi` ,  `pack`.`idPack`, `pack`.`AvaPack`, `oficinas`.`DirOfi`  
    FROM operador LEFT JOIN  `viv`.`oficinas` ON  `operador`.`idOpe` =  `oficinas`.`OPERADOR_idOpe` LEFT JOIN  `viv`.`pack` ON  `oficinas`.`idOfi` =  `pack`.`OFICINAS_idOfi` WHERE ((idPack =$idPack))";


    $result = CModelo::getInstance()->getData($query);
    foreach($result as $r=>$c){
        $table1 = $c;
        foreach($table1 as $r1 => $c1){
            $operador[] = $c1;
        }
    }

//echo "<t  d> <a href='$ruta/MDTourGeneral/CTourEdit.php?codigo=$i'>$numpaxa[0]</a>  </td>";
echo "<td><a href='//modificar.php?operador=$operador[2]'> $operador[0] </a> </td>";
echo "<td><a href='//modificar.php?operador=$operador[1]'> $operador[6] </a> </td>";


if($default==1){
    echo $i;
    echo "<td> <input type=button onClick=\"parent.location='$ruta/MDItinerary/MDEItinerary.php?codigo=$i'\" value='Itinerario del paquete $i'>  </td>";
    echo "<td> <input type=button onClick=\"parent.location='$ruta/MDTourGeneral/CTourEdit.php?codigo=$paquete&default=1'\" value='Crear tour'>  </td>";
    }

else{
    echo "<td><a href='$ruta/MDTourGeneral/MDPasajeros/MDPasajeros.php?codigo=$i'> $numero </a> pasajeros </td>";

    echo "<td> <input type=button onClick=\"parent.location='$ruta/MDItinerary/MDEItinerary.php?codigo=$i'\" value='Itinerario'>  </td>";
}
    echo "<td> <a href='$ruta/MDTourGeneral/CTourEdit.php?codigo=$i'> <img src='../images/edit.png' width='32' title='Editar restaurante $i'/></a> </td>";
    echo "<td><a href='/christian/delete.php?codigo=$i'> <img src='../images/trash.png' width='32' title='Eliminar restaurante $i'/> </a> </td>";
    
    echo "</tr>";

$operador="";
    }





else
    {
       $i2="";$cont=0;$j=0;
        foreach($valor as $id=>$val){
            if($cont==0){$i2=$val;}
            if($cont==1){$paquete=$val;}
            if($cont==4){$newDate=strtotime($val);}
            if($cont==5){$newDate2=strtotime($val);} 
    
                if($j==0){                    
                    if($newDate < $today && $today < $newDate2){    ////  newDate <  $today < NewDate2
                        echo "<tr class='ontransit'>";}
                    elseif($newDate2<$today){
                        echo "<tr class='finished'>";
                    }                            
                    else{
                        echo "<tr class='pending'>";}
                }
                else{
                    $j=0;
                }
                
        echo "<td>$val</td>";$cont=$cont+1;
        $j=1;
        }


$numpax = "select count(*) from tour_has_pasajero where tour_idTour=$i2";
$numpaxa = CModelo::getInstance()->getData($numpax);

    //Conteo de pasajeros del tour
    foreach($numpaxa as $r=>$c){
        $table=$c;
        foreach($table as $r2=>$c2){
            $numero = $c2;
        }
    }

$tour = "SELECT pack_idPack from pack_has_tour where IdPackTour=$i2"; 
$idTour = CModelo::getInstance()->getData($tour);

    foreach($idTour as $r=>$c){
        $table=$c;
        foreach($table as $r2=>$c2){
            $idPack = $c2;
        }
    
    }

    $query = "SELECT  `operador`.`NamOpe` ,  `oficinas`.`idOfi` ,  `oficinas`.`OPERADOR_idOpe` ,  `pack`.`OFICINAS_idOfi` ,  `pack`.`idPack`, `pack`.`AvaPack`, `oficinas`.`DirOfi`  
    FROM operador LEFT JOIN  `viv`.`oficinas` ON  `operador`.`idOpe` =  `oficinas`.`OPERADOR_idOpe` LEFT JOIN  `viv`.`pack` ON  `oficinas`.`idOfi` =  `pack`.`OFICINAS_idOfi` WHERE ((idPack =$idPack))";


    $result = CModelo::getInstance()->getData($query);
    foreach($result as $r=>$c){
        $table1 = $c;
        foreach($table1 as $r1 => $c1){
            $operador[] = $c1;
        }
    }

//echo "<t  d> <a href='$ruta/MDTourGeneral/CTourEdit.php?codigo=$i'>$numpaxa[0]</a>  </td>";
echo "<td><a href='//modificar.php?operador=$operador[2]'> $operador[0] </a> </td>";
echo "<td><a href='//modificar.php?operador=$operador[1]'> $operador[6] </a> </td>";


if($default==1){
    echo $i2;
    echo "<td> <input type=button onClick=\"parent.location='$ruta/MDItinerary/MDEItinerary.php?codigo=$i2'\" value='Itinerario del paquete $i2'>  </td>";
    echo "<td> <input type=button onClick=\"parent.location='$ruta/MDTourGeneral/CTourEdit.php?codigo=$paquete&default=1'\" value='Crear tour'>  </td>";
    
 
}
else{
    echo "<td><a href='$ruta/MDTourGeneral/MDPasajeros/MDPasajeros.php?codigo=$i2'> $numero </a> pasajeros </td>";

    echo "<td> <input type=button onClick=\"parent.location='$ruta/MDItinerary/MDEItinerary.php?codigo=$i2'\" value='Itinerario'>  </td>";
}
    echo "<td> <a href='$ruta/MDTourGeneral/CTourEdit.php?codigo=$i2'> <img src='../images/edit.png' width='32' title='Editar restaurante $i'/></a> </td>";
    echo "<td><a href='/christian/delete.php?codigo=$i2'> <img src='../images/trash.png' width='32' title='Eliminar restaurante $i'/> </a> </td>";
    echo "</tr>";

$operador="";
    }

}
echo "</td></tr></tbody></table><br><br></form>";


echo "</div>";

} 



}
?>